package com.fanbo.service.impl;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.annotation.Transactional;
import com.fanbo.dao.AccountDao;
import com.fanbo.entity.Account;
import com.fanbo.service.TransferService;

/**
 * @author fanbo@imoran.net
 * @date 2020/12/27 20:29
 */
@Service(value = "transferService")
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao ;



    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {


        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        accountDao.updateAccountByCardNo(to);

        if(from.getMoney() < money){
            throw new RuntimeException("余额不足");
        }

        accountDao.updateAccountByCardNo(from);

    }

}
